package com.danco.training.zaneuskaya.dependency;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;

import org.apache.log4j.Logger;


import com.danco.training.zaneuskaya.property.PropertyAccess;
import com.danco.training.zaneuskaya.property.PropertyReader;

public class DependencyConfiguration implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3502084704829594490L;

	private static final Logger logger = Logger
			.getLogger(DependencyConfiguration.class);

	public static DependencyContainer dependencyContainer = DependencyContainer
			.getDependencyContainerInstance();

	public static void fillContainer() {
		HashMap<String, String> result = dependencyContainer.getServices();
		
		PropertyReader.filename = "dependency.properties";
		Enumeration<?> e = PropertyReader.getPropertyReaderInstance().getProps().propertyNames();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			String value = PropertyAccess.getProperty(key);
			result.putIfAbsent(key, value);
		
		}
 
		
	
		dependencyContainer.setServices(result);

	}

	
	public static Object getInstanceByClassName(Class className) {
		
		String classNameString = className.getCanonicalName(); 
		PropertyReader.filename = "dependency.properties";
		
		String instanceClassName = PropertyAccess.getProperty(classNameString) ;
		
		Object object = null;
		try {
			object = Class.forName(instanceClassName).newInstance();
		} catch (InstantiationException e) {
			logger.error(e.getMessage(), e);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage(), e);
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(), e);
		}
		return object;

	}
	
	public static String getFullName(String name){
        
		
		String fullName = PropertyAccess.getProperty(name) ;

		return fullName;
	}

}
